<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
    <title>代理商管理系统</title>
    <link rel="stylesheet" type="text/css" href="${ctx}/statics/easyui/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="${ctx}/statics/easyui/themes/icon.css" />
    <link rel="stylesheet" type="text/css" href="${ctx}/statics/easyui/themes/color.css" />
    <link rel="stylesheet" type="text/css" href="${ctx}/statics/easyui/themes/demo.css" />
    <script src="${ctx}/statics/easyui/jquery.min.js" type="text/javascript"></script>
    <script src="${ctx}/statics/easyui/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="${ctx}/statics/easyui/easyui-lang-zh_CN.js" type="text/javascript"></script>
</head>
<body class="easyui-layout" >
    <div id="tool" data-options="region:'north',border:false" style="width:100%;height:50px;line-height:50px;background:#eee;text-align:center">
        <a class="easyui-linkbutton" data-options="iconCls:'icon-add',size:'small'" style="margin-right:20px" onclick="addUser()">新增用户</a>
        <span style="font-size:13px">搜索客户:</span>
        <input id="search" style="width:200px;height:24px;" />
    </div>
    <div id="data" data-options="region:'center',border:false" style="width:100%;" >
        <table id="dg" style="width:100%;"></table>
    </div>
    <div id="winLog" class="easyui-window" style="width:1000px;height:500px;" title="日志"
         data-options="iconCls:'icon-save',modal:true,closed:true">
        <div class="easyui-layout" data-options="fit:true">
            <div data-options="region:'north',split:false" style="height:40px;line-height:38px;text-align:center" >
                <span>操作时间:</span><input id="logDate" name="logDate" class="easyui-datebox" />
                <a href="javascript:void(0)" class="easyui-linkbutton" style="height:22px;" id="searchLog" >查询</a>
            </div>
            <div data-options="region:'center'" style="text-align:center">
                <table id="logsDg" style="width:100%;"></table>
            </div>
        </div>
    </div>
    <div id="winPreCharge" class="easyui-window" style="width:1000px;height:500px;" title="账户明细"
         data-options="iconCls:'icon-save',modal:true,closed:true">
        <div class="easyui-layout" data-options="fit:true">
            <div data-options="region:'north',split:false" style="height:40px;line-height:38px;text-align:center" >
                <span style="font-size:13px">操作类型:</span>
                <input id="type" name="detailType" style="width:200px;" />
                <span style="font-size:13px">操作时间:</span>
                <input class="easyui-datetimebox" id="start"
                       data-options="showSeconds:true" style="width:150px;height:24px">
                <span style="font-size:13px">至:</span>
                <input class="easyui-datetimebox" id="end"
                       data-options="showSeconds:true" style="width:150px;height:24px;">
                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search'"
                   id="find" style="width:80px;height:24px">查询</a>
            </div>
            <div data-options="region:'center'" style="text-align:center">
                <table id="agentPreDg" style="width:100%;"></table>
            </div>
        </div>
    </div>
    <div id="winUser" class="easyui-window" style="width:450px;height:320px;"
         data-options="iconCls:'icon-save',modal:true,closed:true">
        <div class="easyui-layout" data-options="fit:true">
            <div data-options="region:'north',split:false" style="height:40px;line-height:38px;text-align:center" ><span id="funcTitle"></span></div>
            <div data-options="region:'center'" style="text-align:center">
                <form id="userForm" method="post" >
                    <table id="ct" style="margin:20px auto;" cellpadding="5px">
                        <tr>
                            <td>角色昵称:</td>
                            <td><input id="userCode" class="easyui-textbox" type="text" name="userCode" style="width:120px"
                                       data-options="required:true,missingMessage:'必填'" /></td>
                        </tr>
                        <tr>
                            <td>角色名:</td>
                            <td><input id="userName" class="easyui-textbox" type="text" name="userName" style="width:120px"
                                       data-options="required:true,missingMessage:'必填'" /></td>
                        </tr>
                        <tr>
                            <td>登陆密码:</td>
                            <td><input id="userPassword" class="easyui-textbox" type="text" name="userPassword" style="width:120px"
                                       data-options="required:true,missingMessage:'必填'" />
                                <span style="color:red">默认初始密码123456</span>
                            </td>
                        </tr>
                        <tr>
                            <td>角色:</td>
                            <td>
                                <input id="roleName" style="width:120px" />
                                <input name="roleId" type="hidden" />
                            </td>
                        </tr>
                        <tr>
                            <td>是否启用:</td>
                            <td>
                                <select id="isStart" class="easyui-combobox" name="isStart" style="width:120px;">
                                    <option value='true' selected >启用</option>
                                    <option value='false' >不启用</option>
                                </select>
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
            <div data-options="region:'south',border:false" style="text-align:right;height:40px;padding:7px 10px 5px">
                <a id="confirm" class="easyui-linkbutton" data-options="iconCls:'icon-ok',size:'small'" href="javascript:void(0)"
                   style="width:80px">确定</a>
                <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel',size:'small'" href="javascript:void(0)"
                   onclick="closeWin()" style="width:80px">取消</a>
            </div>
        </div>
    </div>
    <script>
        //页面加载请求当前登陆用户的客户列表
        $(
            function ()
            {
                $("#dg").datagrid
                (
                    {
                        url:'${ctx}/do/showUserManage',
                        fitColumns:true,
                        striped:true,
                        fit:true,
                        rownumbers:true,
                        pagination:true, //增加分页工具栏
                        pageNumber:1, //设置初始化页码
                        pageSize:10,  //页大小
                        pageList:[5,10,15], //显示数量选择
                        columns:
                            [
                                [
                                    {field:'userName',title:'用户名称',width:'15%',align:'center'},
                                    {field:'roleName',title:'角色',width:'15%',align:'center'},
                                    {field:'creationTime',title:'创建时间',width:'15%',align:'center'},
                                    {field:'isStart',title:'是否启用',width:'15%',align:'center',
                                        formatter: function(value,row,index)
                                        {
                                            return ( value?"启用":"未启用" );
                                        },
                                        styler: function(value,row,index)
                                        {
                                            return ( value?"color:green":"color:red" );
                                        }
                                    },
                                    {field:'control',title:'操作', width:'38%', align:'center',
                                        formatter: function(value,row,index)
                                        {
                                            str = "<a href='javascript:void(null)' class='easyui-linkbutton' onclick='update(" + row.id + ")' " +
                                                " data-options='iconCls:\"icon-edit\",size:\"small\"'  >修改</a>" + "&nbsp;&nbsp;|&nbsp;&nbsp;" +
                                                "<a href='javascript:void(null)' class='easyui-linkbutton' onclick='del(" + row.id + ")' " +
                                                " data-options='iconCls:\"icon-edit\",size:\"small\"'  >删除</a>" + "&nbsp;&nbsp;|&nbsp;&nbsp;" +
                                                "<a href='javascript:void(null)' class='easyui-linkbutton' onclick='viewLog(" + row.id + ")' " +
                                                " data-options='iconCls:\"icon-edit\",size:\"small\"'  >LOG日志</a>" + "&nbsp;&nbsp;|&nbsp;&nbsp;" +
                                                "<a href='javascript:void(null)' class='easyui-linkbutton' onclick='preCharge(" + row.id + ")' " +
                                                " data-options='iconCls:\"icon-edit\",size:\"small\"'  >预付款</a>";
                                            return str;
                                        }
                                    }
                                ]
                            ]
                    }
                );

                //设置角色combobox样式
                $("#roleName").combobox(
                    {
                        url:'roleComboboxJsonStr',
                        valueField: 'id',
                        textField: 'roleName',
                        onSelect: function (rec)
                        {
                            var roleId = $("#roleName").combobox('getValue');
                            $("[name=roleId]").val( roleId );
                        }
                    });

                //点击搜索客户根据客户名进行模糊搜索
                $("#search").textbox(
                    {
                        buttonText:'搜索',
                        buttonIcon:'icon-search',
                        prompt:'请输入客户名搜索',
                        onClickButton:function ()
                        {
                            //获取要搜索的客户名称
                            var customName = $("#search").textbox('getValue');
                            //将输入的客户名加入请求参数中
                            //因customDatagrid的原设置中已包含url,故无需reload
                            customDatagrid.datagrid(
                                {
                                    queryParams: {"customName":customName}
                                }
                            );
                        }
                    }
                );

            }
        );

        //点击查看日志
        function viewLog( id )
        {
            //为查询按钮注册事件
            $("#searchLog").off('click').on('click' ,
                function()
                {
                    //获取输入的查询时间
                    var logDate = $("#logDate").datebox('getValue');
                    //根据指定条件重新查询
                    logsDg.datagrid( { queryParams:{"userId":id,"logDate":logDate} } );
                }
            );
            //发送请求查询用户日志
            var logsDg = $("#logsDg").datagrid(
                {
                    url:'${ctx}/do/showLogsOfUser',
                    queryParams:{"userId":id},
                    singleSelect:true,
                    fitColumns:true,
                    striped:true,
                    fit:true,
                    rownumbers:true,
                    pagination:true, //增加分页工具栏
                    pageNumber:1, //设置初始化页码
                    pageSize:10,  //页大小
                    pageList:[5,10,15], //显示数量选择
                    columns:
                        [
                            [
                                {field:'id',title:'序号',width:'10%',align:'center'},
                                {field:'userName',title:'用户名称',width:'10%',align:'center'},
                                {field:'operateInfo',title:'操作信息',width:'40%',align:'center'},
                                {field:'operateDatetime',title:'操作时间',width:'28%',align:'center'}
                            ]
                        ]
                });
            //开启日志窗口
            $("#winLog").window('open');
        }

        //查看预付款
        function preCharge( id )
        {
            //设置用户存在的交易类型选项
            $("#type").combobox(
                {
                    url:'${ctx}/do/comboboxJsonStr',
                    queryParams:{"action":"detailType","userId":id},
                    valueField:'detailType',
                    textField:'detailTypeName'
                });

            //为查询按钮注册事件
            $("#find").linkbutton(
                {
                    onClick:function()
                    {
                        var detailType = $("#type").combobox('getValue');
                        //alert(detailType);
                        //获取时间段
                        var start = $("#start").datetimebox('getValue');
                        //alert(start);
                        //(默认获取为string类型)不输入查询时间时将该值转换为null
                        var end = $("#end").datetimebox('getValue');
                        //alert(end);
                        if( detailType != "" )
                        {
                            if( start != "" && end != "" )
                            {
                                //加入请求参数
                                agentPreDg.datagrid(
                                    {
                                        queryParams:{"start":start,"end":end,"detailType":detailType}
                                    }
                                );
                            }
                            else
                            {
                                //加入请求参数
                                agentPreDg.datagrid(
                                    {
                                        queryParams:{"detailType":detailType}
                                    }
                                );
                            }
                        }
                        else
                        {
                            if( start != "" && end != "" )
                            {
                                //加入请求参数
                                agentPreDg.datagrid(
                                    {
                                        queryParams:{"start":start,"end":end}
                                    }
                                );
                            }
                            else
                            {
                                agentPreDg.datagrid(
                                    {
                                        queryParams:{"start":null,"end":null}
                                    }
                                );
                            }
                        }
                    }
                }
            );

            //读取预付款列表数据
            var agentPreDg = $("#agentPreDg").datagrid({
                url:'${ctx}/do/showAgentPrePay',
                queryParams:{"userId":id},
                singleSelect:true,
                fitColumns:true,
                striped:true,
                fit:true,
                rownumbers:true,
                pagination:true, //增加分页工具栏
                pageNumber:1, //设置初始化页码
                pageSize:10,  //页大小
                pageList:[5,10,15], //显示数量选择
                columns:
                    [
                        [
                            {field:'money',title:'账务资金',width:'9%',align:'center'},
                            {field:'accountMoney',title:'账户余额',width:'9%',align:'center'},
                            {field:'memo',title:'备注信息',width:'55%',align:'center'},
                            {field:'detailDateTime',title:'交易时间',width:'20%',align:'center'}
                        ]
                    ]
            });
            //开启预付款窗口
            $("#winPreCharge").window('open');
        }

        //新增用户
        function addUser()
        {
            //清空表单
            $('#userForm').form('reset');
            //设置窗口标题
            $("#funcTitle").text('添加用户信息');
            //为确定按钮注册事件
            $("#confirm").off('click').on('click',
                function()
                {
                    //提交表单
                    $('#userForm').form('submit',
                        {
                            url:"${ctx}/do/doAddUser",
                            success:function(data)
                            {
                                //刷新datagrid
                                $("#dg").datagrid('reload');
                            }
                        });
                    //关闭窗口
                    $("#winUser").window('close');
                    //清空表单
                    $('#userForm').form('reset');
                });
            //开启添加用户窗口
            $("#winUser").window('open');
        }

        //修改用户
        function update( id )
        {
            //清空表单
            $('#userForm').form('reset');
            //设置窗口标题
            $("#funcTitle").text('修改用户信息');
            //发送请求获取指定用户信息用户设置表单
            $.ajax(
                {
                    type:"POST",
                    url:"${ctx}/do/doGetUserById",
                    data:{"id":id},
                    dataType:"json",
                    success:function( json )
                    {
                        //设置表单数据
                        $('#userForm').form('load',
                            {
                                userCode:json.userCode,
                                userName:json.userName,
                                userPassword:json.userPassword,
                                roleId:json.roleId,
                                isStart:json.isStart + ""
                            }
                        );
                        $("#roleName").combobox('select',json.roleId);
                    }
                }
            );
            //为确定按钮注册事件
            $("#confirm").off('click').on('click',
                function()
                {
                    //提交表单
                    $('#userForm').form('submit',
                        {
                            url:"${ctx}/do/doUpdateUser",
                            queryParams:{"id":id},
                            success:function(data)
                            {
                                //刷新datagrid
                                $("#dg").datagrid('reload');
                            }
                        });
                    //关闭窗口
                    $("#winUser").window('close');
                    //清空表单
                    $('#userForm').form('reset');
                });

            //开启修改用户窗口
            $("#winUser").window('open');

        }

        //关闭窗口
        function closeWin()
        {
            $("#winUser").window('close');
        }

        //删除用户
        function del( id )
        {
            $.messager.confirm('删除用户','您确定要删除该用户吗？',
                function(r)
                {
                    if (r)
                    {
                        //发送请求删除该用户
                        $.ajax(
                            {
                                type:"POST",
                                url:"${ctx}/do/doDeleteUser",
                                data:{"id":id},
                                dataType:"json",
                                success:function( json )
                                {
                                    if( json.success )
                                    {
                                        $.messager.alert('删除用户','删除成功','info');
                                    }
                                    else
                                    {
                                        $.messager.alert('删除用户','删除失败','warning');
                                    }
                                    //刷新datagrid
                                    $("#dg").datagrid('reload');
                                }
                            }
                        )
                    }
                });
            }

    </script>

</body>
</html>
